﻿using Microsoft.AspNetCore.SignalR;
using Testudo.Models;
using Testudo.Services;
using Testudo.WebApp.Events;

namespace Testudo.WebApp.Hubs;

public class WcsProxyHub : Hub<IScenarioClient>
{
    public WcsProxyHub(IWcsProxyService service)
    {
        Service = service;
        Service.ScenarioChanged += Service_ScenarioChanged;
    }

    private void Service_ScenarioChanged(object sender, ScenarioConfig scenario)
    {
        if (scenario != null)
        {
            Clients.All.Notify(new NotifyCommand
            {
                Cmd = new CommandLoad
                {
                    ScenarioId = scenario.Id,
                    Message = "ScenarioLoaded"
                }
            });
        }
    }

    private IWcsProxyService Service { get; }

    public override async Task OnConnectedAsync()
    {
        await base.OnConnectedAsync();
        if (Service.Scenario != null && Guid.Empty != Service.Scenario.Id)
        {
            await Clients.All.Notify(new NotifyCommand
            {
                Cmd = new CommandLoad
                {
                    ScenarioId = Service.Scenario.Id,
                    Message = "ScenarioLoaded"
                }
            });
        }
    }

    public async Task Load(Guid id)
    {
        try
        {
            await Service.LoadScenario(id);
            await Clients.All.Notify(new NotifyCommand
            {
                Cmd = new CommandLoad
                {
                    ScenarioId = id,
                    Message = "ScenarioLoaded"
                }
            });
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            await Clients.All.Notify(new NotifyError());
        }
    }

    public async Task Unload(Guid id)
    {
        try
        {
            await Service.UnloadScenario(id);
            await Clients.All.Notify(new NotifyCommand
            {
                Cmd = new CommandUnload
                {
                    ScenarioId = id,
                    Message = $"Unloaded {id}"
                }
            });
        }
        catch (Exception)
        {
            await Clients.All.Notify(new NotifyError());
        }
    }

    public async Task Start(Guid id)
    {
        try
        {
            await Service.Start(id);
            await Clients.All.Notify(new NotifyCommand
            {
                Cmd = new CommandStart
                {
                    ScenarioId = id,
                    Message = $"Start {id}"
                }
            });
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            await Clients.All.Notify(new NotifyError());
        }
    }

    public async Task Stop(Guid id)
    {
        try
        {
            await Service.Stop(id);
            await Clients.All.Notify(new NotifyCommand
            {
                Cmd = new CommandStop
                {
                    ScenarioId = id,
                    Message = $"Stop {id}"
                }
            });
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            await Clients.All.Notify(new NotifyError());
        }
    }
}